using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Configuration;

namespace VirtualScreenMaximizer
{
	public partial class Form1 : Form
	{



		Maximizer.ConfiguredKey maxKey;
		Maximizer.ConfiguredKey resKey;

		System.Configuration.Configuration config = null;
		public Form1()
		{
			InitializeComponent();

			config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
			if (config.AppSettings.Settings ["maximize"] != null)
			{
				string maxS = config.AppSettings.Settings ["maximize"].Value;
				if (maxS != null)
				{
					string [] maxP = maxS.Split(new char [1] { ' ' }, 2);

					if (maxP.Length > 0)
					{
						if (maxP [0].Length >= 5)
						{
							maxKey = new Maximizer.ConfiguredKey(maxP [0] [0] == '1', maxP [0] [1] == '1', maxP [0] [2] == '1', Convert.ToInt32(maxP [0].Substring(3)));
						}
						if (maxP.Length > 1)
						{
							textBoxMaxHK.Text = maxP [1];
						}
					}
				}

			}

			if (config.AppSettings.Settings ["restore"] != null)
			{
				string resS = config.AppSettings.Settings ["restore"].Value;
				if (resS != null)
				{
					string [] resP = resS.Split(new char [1] { ' ' }, 2);

					if (resP.Length > 0)
					{
						if (resP [0].Length >= 5)
						{
							resKey = new Maximizer.ConfiguredKey(resP [0] [0] == '1', resP [0] [1] == '1', resP [0] [2] == '1', Convert.ToInt32(resP [0].Substring(3)));
						}
						if (resP.Length > 1)
						{
							textBoxResHK.Text = resP [1];
						}
					}
				}
			}
		}



		protected override void WndProc(ref Message m)
		{
			if (m.Msg == 0x312)	//WM_HOTKEY
			{
				Maximizer.ProcessHK(m.WParam);

				return;
			}
			base.WndProc(ref m);
		}



		private void Form1_Load(object sender, EventArgs e)
		{
			Init();
		}

		private void textBoxMaxHK_KeyDown(object sender, KeyEventArgs e)
		{
			if (e.Modifiers != Keys.None)
				textBoxMaxHK.Text = e.Modifiers + " " + e.KeyCode;
			else
				textBoxMaxHK.Text = e.KeyCode.ToString();
			textBoxMaxHK.Tag = e;
		}

		private void textBoxResHK_KeyDown(object sender, KeyEventArgs e)
		{
			if (e.Modifiers != Keys.None)
				textBoxResHK.Text = e.Modifiers + " " + e.KeyCode;
			else
				textBoxResHK.Text = e.KeyCode.ToString();
			textBoxResHK.Tag = e;

		}

		private void buttonSave_Click(object sender, EventArgs e)
		{

			config.AppSettings.Settings.Remove("maximize");
			KeyEventArgs max = (KeyEventArgs) textBoxMaxHK.Tag;
			if (max != null)
			{
				string maxS = "";
				maxS += max.Alt ? "1" : "0";
				maxS += max.Control ? "1" : "0";
				maxS += max.Shift ? "1" : "0";
				maxS += max.KeyValue;
				maxS += " " + textBoxMaxHK.Text;
				config.AppSettings.Settings.Add("maximize", maxS);
				maxKey = new Maximizer.ConfiguredKey(max.Alt, max.Control, max.Shift, max.KeyValue);
			}

			config.AppSettings.Settings.Remove("restore");
			KeyEventArgs res = (KeyEventArgs) textBoxResHK.Tag;
			if (res != null)
			{
				string resS = "";
				resS += res.Alt ? "1" : "0";
				resS += res.Control ? "1" : "0";
				resS += res.Shift ? "1" : "0";
				resS += res.KeyValue;
				resS += " " + textBoxResHK.Text;
				config.AppSettings.Settings.Add("restore", resS);
				resKey = new Maximizer.ConfiguredKey(res.Alt, res.Control, res.Shift, res.KeyValue);
			}


			config.Save(ConfigurationSaveMode.Minimal, true);
			Init();
			this.Hide();

		}


		public void Init()
		{
			Maximizer.RegisterHK(Handle, GetHashCode(), (Keys) maxKey.KeyCode,
				(maxKey.Alt ? Maximizer.MOD_ALT : 0) |
				(maxKey.Control ? Maximizer.MOD_CONTROL : 0) |
				(maxKey.Shift ? Maximizer.MOD_SHIFT : 0), "FullScreen");

			Maximizer.RegisterHK(Handle, GetHashCode(), (Keys) resKey.KeyCode,
				(resKey.Alt ? Maximizer.MOD_ALT : 0) |
				(resKey.Control ? Maximizer.MOD_CONTROL : 0) |
				(resKey.Shift ? Maximizer.MOD_SHIFT : 0), "RestoreOriginal");

		}

		private void configToolStripMenuItem_Click(object sender, EventArgs e)
		{
			this.Show();
		}

		private void quitToolStripMenuItem_Click(object sender, EventArgs e)
		{
			Application.Exit();
		}

		private void Form1_Shown(object sender, EventArgs e)
		{
			this.Hide();
		}

	}
}